package com.example.heimashop.view.adapter;

import android.animation.ObjectAnimator;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.heimashop.databinding.ItemProductBinding;
import com.example.heimashop.model.Product;
import com.example.heimashop.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 商品列表适配器
 * 使用ViewBinding和Material Design优化的现代化适配器
 */
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {

    private List<Product> productList = new ArrayList<>();
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Product product);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<Product> list) {
        if (list != null) {
            this.productList = list;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemProductBinding binding = ItemProductBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Product product = productList.get(position);
        holder.bind(product, listener);

        // 添加进入动画效果
        holder.itemView.setAlpha(0f);
        holder.itemView.setTranslationY(50f);
        holder.itemView.animate()
                .alpha(1f)
                .translationY(0f)
                .setDuration(300)
                .setStartDelay(position * 50L)
                .start();
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    /**
     * ViewHolder使用ViewBinding
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemProductBinding binding;

        ViewHolder(ItemProductBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(Product product, OnItemClickListener listener) {
            // 设置商品名称
            binding.tvName.setText(product.getGoodsName());

            // 设置价格（只显示数字，货币符号在布局中）
            binding.tvPrice.setText(String.format(Locale.CHINA, "%.2f", product.getGoodsPrice()));

            // 设置库存
            binding.tvSales.setText(String.format(Locale.CHINA, "库存%d", product.getGoodsNumber()));

            // 加载商品图片，优先使用小图
            String imageUrl = product.getGoodsSmallLogo();
            if (imageUrl == null || imageUrl.isEmpty()) {
                imageUrl = product.getGoodsBigLogo();
            }
            if (imageUrl != null && !imageUrl.isEmpty()) {
                GlideUtils.loadImage(binding.getRoot().getContext(), imageUrl, binding.ivProduct);
            }

            // 设置点击事件
            binding.getRoot().setOnClickListener(v -> {
                if (listener != null) {
                    // 添加点击动画效果
                    addClickAnimation(v);
                    listener.onItemClick(product);
                }
            });
        }

        /**
         * 添加点击缩放动画效果
         */
        private void addClickAnimation(android.view.View view) {
            view.animate()
                    .scaleX(0.95f)
                    .scaleY(0.95f)
                    .setDuration(100)
                    .withEndAction(() ->
                        view.animate()
                                .scaleX(1f)
                                .scaleY(1f)
                                .setDuration(100)
                                .start()
                    )
                    .start();
        }
    }
}
